package com.ruitu.health_2.ui.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hjq.permissions.Permission;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;
import com.ruitu.arad.util.PhoneUtils;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.health_2.R;
import com.ruitu.router_module.bean.product.ArtMan;
import com.ruitu.router_module.util.PermissionUtil;

import androidx.fragment.app.FragmentActivity;

// 模块下的艺人列表adapter
public class ModelArtManListAdapter extends ListBaseAdapter<ArtMan> {

    public int type = 0;// 0 双列 1 单列

    public ModelArtManListAdapter(Context context) {
        super(context);
    }

    @Override
    public int getLayoutId() {
//        if (type == 1) {// 单列
//            return R.layout.item_art_man_list;
//        }
        return R.layout.item_model_art_man_list;
    }

    @Override
    public void onBindItemHolder(SuperViewHolder holder, int position) {
        ImageView icon = holder.getView(R.id.icon);
        ImageView iv_call = holder.getView(R.id.iv_call);
        TextView tv_title = holder.getView(R.id.tv_title);// 艺人姓名
        TextView tv_name = holder.getView(R.id.tv_name);
        TextView tv_subtitle = holder.getView(R.id.tv_subtitle);// 艺人介绍

//        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) icon.getLayoutParams();
//        params.height = SizeUtils.dp2px(80);
//        if (type == 1) {// 单列 艺人的头像宽度大一些
//            params.width = params.height * 16 / 10;
//        } else {
//            params.width = params.height;
//        }
//        icon.setLayoutParams(params);

        final ArtMan man = mDataList.get(position);
        tv_title.setText(man.getName());
        tv_name.setText(man.getBrokerageName());
        tv_subtitle.setText(TextUtils.isEmpty(man.getIntro()) ? "暂无标签" : man.getIntro());

//        GlideUtil.loadImg(man.getAvaterUrl(), icon);
        Glide.with(Arad.app).load(man.getAvaterUrl()).apply(Arad.opts_circle).into(icon);

        final String phoneNum = man.getPhone();
        if (TextUtils.isEmpty(phoneNum)) {
            iv_call.setImageResource(R.mipmap.icon_call_02);
        } else {
            iv_call.setImageResource(R.mipmap.icon_call_01);
        }
        iv_call.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionUtil.handleSinglePermission((FragmentActivity) mContext, Permission.CALL_PHONE, "拨打电话", new PermissionUtil.PermissionListener() {
                    @Override
                    public void onPermissionOk(boolean isOk) {
                        if (isOk) {
                            if (!TextUtils.isEmpty(phoneNum)) {
                                try {
                                    PhoneUtils.dial(phoneNum);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    ToastUtils.showShortSafe("无权限或设备不支持拨号");
                                }
                            } else {
                                ToastUtils.showShortSafe("该艺人电话未公开");
                            }
                        }
                    }

                    @Override
                    public void onOpenPermissionPage(boolean isOpen) {

                    }
                });
            }
        });
    }
}
